var fs, path, stringify;

require('colors');

fs = require('fs-extra');

path = require('path-extra');

stringify = function(str) {
  if (typeof str === "undefined") {
    return "undefined";
  }
  if (typeof str === 'string') {
    return str;
  }
  if (str.toString().startsWith('[object ')) {
    str = JSON.stringify(str);
  } else {
    str = str.toString();
  }
  return str;
};

module.exports = {
  remoteStringify: JSON.stringify,
  log: function(str) {
    str = stringify(str);
    return console.log("[INFO] " + str);
  },
  warn: function(str) {
    str = stringify(str);
    return console.warn(("[WARN] " + str).yellow);
  },
  error: function(str) {
    str = stringify(str);
    return console.error(("[ERROR] " + str).bold.red);
  },
  setBounds: function(options) {
    return global.mainWindow.setBounds(options);
  },
  getBounds: function() {
    return global.mainWindow.getBounds();
  },
  capturePageInMainWindow: function(rect, scpath, callback) {
    return global.mainWindow.capturePage(rect, function(image) {
      var buf, date, e, error, filename, now;
      try {
        buf = image.toPng();
        now = new Date();
        date = (now.getFullYear()) + "-" + (now.getMonth() + 1) + "-" + (now.getDate()) + "T" + (now.getHours()) + "." + (now.getMinutes()) + "." + (now.getSeconds());
        fs.ensureDirSync(scpath);
        filename = path.join(scpath, date + ".png");
        return fs.writeFile(filename, buf, function(err) {
          return callback(err, filename);
        });
      } catch (error) {
        e = error;
        return callback(err);
      }
    });
  }
};
